package com.hxzy.dao.impl;

import com.hxzy.dao.DogDao;
import com.hxzy.pojo.Dog;
import com.hxzy.utils.JDBCUtils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author wzm
 * @create 2023-09-22 9:28
 * project_name:javaweb2308
 */
//dao的实现类
public class DogDaoImpl implements DogDao {
   private QueryRunner queryRunner = new QueryRunner();
   private Connection conn = null;

    @Override
    public int insertDog(Dog dog) {
        try {
            //连接 sql dbutils
            //1.打开与数据库连接
             conn = JDBCUtils.getConn();
            //2.书写SQL语句
            String sql = "insert into dog(name,age,sex) values(?,?,?)";
            //3.执行操作
            return queryRunner.update(conn, sql, dog.getName(), dog.getAge(), dog.getSex());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
        return 0;//失败返回0
    }

    @Override
    public int deleteDog(Dog dog) {
        try {
            //连接 sql dbutils
            //1.打开与数据库连接
            conn = JDBCUtils.getConn();
            //2.书写SQL语句
            String sql = "delete from dog where id = ?";
            //3.执行操作
            return queryRunner.update(conn, sql, dog.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
        return 0;//失败返回0
    }

    @Override
    public int updateDog(Dog dog) {
        try {
            //连接 sql dbutils
            //1.打开与数据库连接
            conn = JDBCUtils.getConn();
            //2.书写SQL语句
            String sql = "update dog set name = ?,age = ?,sex = ? where id = ?";
            //3.执行操作
            return queryRunner.update(conn, sql, dog.getName(), dog.getAge(), dog.getSex(),dog.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
        return 0;//失败返回0
    }

    @Override
    public Dog selectById(Dog dog) {
        try {
            conn = JDBCUtils.getConn();
            String sql = "select * from dog where id = ?";
            return queryRunner.query(conn,sql,new BeanHandler<>(Dog.class),dog.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
        return null;//失败返回null
    }

    @Override
    public List<Dog> selectByAll() {
        try {
            conn = JDBCUtils.getConn();
            String sql = "select * from dog";
            return queryRunner.query(conn,sql,new BeanListHandler<>(Dog.class));
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
        return null;//失败返回null
    }
}
